package com.springclouddemo.eurekaclientconsumerfeignfallbackfactory.feign;

import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author 何昌杰
 */
@Component
@FeignClient(name = "eureka-client-provider", fallbackFactory = FeignClientFallbackFactory.class)
public interface ProviderFeign {

    @GetMapping("/hello/{name}")
    String demo1(@PathVariable("name") String name);
}

@Component
class FeignClientFallbackFactory implements FallbackFactory<ProviderFeign>{

    private static final Logger log = LoggerFactory.getLogger(FeignClientFallbackFactory.class);

    @Override
    public ProviderFeign create(Throwable throwable) {
        return new ProviderFeign() {
            /**
             * 将日志置于fallback方法中防止引用启动时输出日志
             * @param name 请求参数
             * @return 响应
             */
            @Override
            public String demo1(String name) {
                if(throwable instanceof IllegalAccessException){
                    //..
                }else if(throwable instanceof ClassCastException){
                    //...
                }
                return "默认返回值";
            }
        };
    }
}